package exercise;

class Solution12 {
    public int minOperations(int[] nums, int x) {
        int len = nums.length, l = 0, r = 0, target = 0, ans = 0;
        for (int i : nums) {
            target += i;
        }
        target -= x;
        int sum = 0;
        if (target < 0) return -1;
        if (target == 0) return len;
        while (r < len) {
            // System.out.println(l + " " + r);
            sum += nums[r];
            while (sum > target) {
                sum -= nums[l++];
            }
            if (sum == target) {
                ans = Math.max(ans, r - l + 1);
            }
            r++;
        }
        return ans == 0 ? -1 : len - ans;
    }
}